package com.dkd.record.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.dkd.common.utils.DateUtils;
import com.dkd.record.domain.HomeCountNumVO;
import com.dkd.record.domain.HomePlayTypeVO;
import com.dkd.record.domain.RankingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.record.mapper.RecordMapper;
import com.dkd.record.domain.Record;
import com.dkd.record.service.IRecordService;

/**
 * 打本记录Service业务层处理
 * 
 * @author ruyi
 * @date 2024-11-08
 */
@Service
public class RecordServiceImpl implements IRecordService
{
    @Autowired
    private RecordMapper recordMapper;

    /**
     * 查询打本记录
     *
     * @param playId 打本记录主键
     * @return 打本记录
     */
    @Override
    public Record selectRecordByPlayId(Long playId)
    {
        return recordMapper.selectRecordByPlayId(playId);
    }

    /**
     * 查询打本记录列表
     *
     * @param record 打本记录
     * @return 打本记录
     */
    @Override
    public List<Record> selectRecordList(Record record)
    {
        return recordMapper.selectRecordList(record);
    }

    /**
     * 新增打本记录
     *
     * @param record 打本记录
     * @return 结果
     */
    @Override
    public int insertRecord(Record record)
    {
        record.setCreateTime(DateUtils.getNowDate());
        return recordMapper.insertRecord(record);
    }

    /**
     * 修改打本记录
     *
     * @param record 打本记录
     * @return 结果
     */
    @Override
    public int updateRecord(Record record)
    {
        record.setUpdateTime(DateUtils.getNowDate());
        return recordMapper.updateRecord(record);
    }

    /**
     * 批量删除打本记录
     *
     * @param playIds 需要删除的打本记录主键
     * @return 结果
     */
    @Override
    public int deleteRecordByPlayIds(Long[] playIds)
    {
        return recordMapper.deleteRecordByPlayIds(playIds);
    }

    /**
     * 删除打本记录信息
     *
     * @param playId 打本记录主键
     * @return 结果
     */
    @Override
    public int deleteRecordByPlayId(Long playId)
    {
        return recordMapper.deleteRecordByPlayId(playId);
    }

    @Override
    public HomeCountNumVO getCountNum(String start, String end) {
        //日期内全部记录数量
        List<Record> list = recordMapper.getCountNum(start, end);
        //统计记录数和总金额
        int count = 0;
        float money = 0;
        for (Record record : list) {
            Long moneys = record.getMoneys();
            money += moneys;
            count++;
        }
        //数据封装
        HomeCountNumVO homeCountNumVO = new HomeCountNumVO();
        homeCountNumVO.setOrderCountNum(count);
        homeCountNumVO.setOrderAmountNum(money);
        return homeCountNumVO;
    }

    @Override
    public HomePlayTypeVO countPlayType(String start, String end) {
        //日期内全部记录数量
        List<Record> records = recordMapper.getCountNum(start, end);
        //统计各个游戏类型下的数量
        Map<String, Long> playTypeCounts = records.stream()
                .collect(Collectors.groupingBy(Record::getPlayType, Collectors.counting()));
        //数据封装
        HomePlayTypeVO homePlayTypeVO = new HomePlayTypeVO();
        homePlayTypeVO.setScriptTotal(playTypeCounts.getOrDefault("0", 0L));
        homePlayTypeVO.setWerewolfTotal(playTypeCounts.getOrDefault("1", 0L));
        homePlayTypeVO.setBloodyTotal(playTypeCounts.getOrDefault("2", 0L));
        homePlayTypeVO.setBoardTotal(playTypeCounts.getOrDefault("3", 0L));
        return homePlayTypeVO;
    }

    @Override
    public List<RankingVO> getRanking(String start, String end) {
        //日期内全部记录数量
        List<Record> records = recordMapper.getCountNum(start, end);

        List<Map.Entry<String, Long>> sortedPlayNameCounts = records.stream()
                .collect(Collectors.groupingBy(Record::getPlayName, Collectors.counting()))
                .entrySet().stream()
                .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
                .collect(Collectors.toList());

        List<RankingVO> rankingVOList = sortedPlayNameCounts.stream()
                .map(e -> new RankingVO(e.getKey(), e.getValue()))
                .collect(Collectors.toList());

        List<RankingVO> topTenList = rankingVOList.size() > 15 ?
                rankingVOList.subList(0, 15) :
                rankingVOList;
        return topTenList;
    }

}
